package com.team.merchant.task;

import com.team.merchant.service.StatsService;
import com.team.merchant.service.WebSocketService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * WebSocket定时推送任务
 */
@Component
@EnableScheduling
@RequiredArgsConstructor
@Slf4j
public class WebSocketScheduledTask {

    private final WebSocketService webSocketService;
    private final StatsService statsService;
    
    /**
     * 每5秒推送一次最新统计数据
     */
    //@Scheduled(fixedRate = 5000)
    public void sendStatsUpdate() {
        try {
            log.info("定时推送WebSocket消息 - 开始");
            webSocketService.sendStatsUpdate(statsService.getLatestStats());
            log.info("定时推送WebSocket消息 - 完成");
        } catch (Exception e) {
            log.error("定时推送WebSocket消息失败: {}", e.getMessage(), e);
        }
    }
} 